package com.jf3q.sup.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf3q.sup.domain.Employees;
import com.jf3q.sup.domain.Users;
import com.jf3q.sup.dto.EmpDto;
import com.jf3q.sup.dto.UserDto;
import com.jf3q.sup.mapper.EmployeesMapper;
import com.jf3q.sup.service.EmployeesService;
import com.jf3q.sup.service.UsersService;
import com.jf3q.sup.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 13709
 * @description 针对表【employees】的数据库操作Service实现
 * @createDate 2024-03-25 18:49:44
 */
@Service
public class EmployeesServiceImpl extends ServiceImpl<EmployeesMapper, Employees>
    implements EmployeesService{
    @Autowired
    EmployeesMapper employeesMapper;

    @Autowired
    UsersService usersService;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public Page<Employees> getPage(EmpDto empDto) {
        Page<Employees> page = employeesMapper.getPage(new Page<Employees>(empDto.getPageNum(),empDto.getPageSize()),empDto);
        return page;
    }

    @Override
    @Transactional
    public ResultVo saveEmp(UserDto userDto) {
        try {
            Users users = new Users();
            users.setStatus(userDto.getStatus());
            users.setPassword(passwordEncoder.encode(userDto.getPassword()));
            users.setUsername(userDto.getUsername());
            usersService.save(users);
            Employees employees = new Employees();
            employees.setUserId(users.getId());
            employees.setAddress(userDto.getAddress());
            employees.setEmail(userDto.getEmail());
            employees.setName(userDto.getName());
            employees.setPhone(userDto.getPhone());
            employees.setDepartmentId(userDto.getDepartmentId());
            employees.setPositionId(userDto.getPositionId());
            employees.setHireDate(new Date());
            save(employees);
            return ResultVo.success("添加成功",null);
        } catch (Exception e) {
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public ResultVo updateEmp(UserDto userDto) {
        try {
            Users users = new Users();
            users.setId(userDto.getUserId());
            users.setStatus(userDto.getStatus());
            users.setUsername(userDto.getUsername());
            users.setPassword(userDto.getPassword());
            usersService.updateById(users);
            Employees employees = new Employees();
            employees.setId(userDto.getId());
            employees.setUserId(users.getId());
            employees.setAddress(userDto.getAddress());
            employees.setEmail(userDto.getEmail());
            employees.setName(userDto.getName());
            employees.setPhone(userDto.getPhone());
            employees.setDepartmentId(userDto.getDepartmentId());
            employees.setPositionId(userDto.getPositionId());
            employees.setHireDate(new Date());
            updateById(employees);
            return ResultVo.success("修改成功",null);
        } catch (Exception e) {
            throw new RuntimeException("修改失败");
        }
    }
}




